﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Server
{
    public partial class Editon : Form
    {
        public Editon()
        {
            InitializeComponent();
        }

        string Path = System.IO.Directory.GetCurrentDirectory() + "\\menu.txt";

        /// <summary>
        /// 创建一个名字为 menu 的菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Editon_Load(object sender, EventArgs e)
        {
            if (!File.Exists(Path))
            {
                MessageBox.Show("未检测到菜品目录，将创建新的菜品目录");
            }

            string[] str = File.ReadAllLines(Path);

            lstB_Menu.Items.Clear();
            foreach(string temp in str)
            {
                lstB_Menu.Items.Add(temp);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(tB_Enter.Text == "")
            {
                return;
            }
            lstB_Menu.Items.Add(tB_Enter.Text);
            tB_Enter.Text = "";
        }

        void ReadMenu()
        {
            while(true)
            {
                StringBuilder temp = new StringBuilder();

                string All_Menu;
                try
                {
                    All_Menu = File.ReadAllText(Path);
                    
                    //Invoke((EventHandler)delegate { tB_Show.Text = All_Menu; });
                }
                catch
                {

                }

                Thread.Sleep(100);
            }
        }

        public void Write(string temp)
        {
            //开始写入
            StreamWriter sw = File.AppendText(System.IO.Directory.GetCurrentDirectory() + "\\menu.txt");

            sw.WriteLine(temp);
            sw.Flush();
            sw.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                lstB_Menu.Items.RemoveAt(lstB_Menu.SelectedIndex);
            }
            catch
            {

            }
            
        }

        private void bt_Done_Click(object sender, EventArgs e)
        {
            FileStream fs = File.OpenWrite(Path);
            fs.SetLength(0);
            fs.Close();
            for(int i=0;i<lstB_Menu.Items.Count;i++)
            {
                Write(lstB_Menu.Items[i].ToString());
            }
            MessageBox.Show("记录完成", "中科浩电点餐系统");
            Close();
        }

        private void lstB_Menu_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
